package com.systemsjr.jrenterprise.user;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.systemsjr.jrenterprise.user.vo.UserDetailsVO;
import com.systemsjr.jrenterprise.utils.ServiceUtils;
import com.systemsjr.jrlib.richclient.BaseItemDialog;

public class UserDialog extends BaseItemDialog<UserDetailsVO> {

	public UserDialog() {
		super("userDialog");
	}
	
	public UserDialog(UserDetailsVO userVO) {
		super("userDialog");
		setItem(userVO);
	}

	@Override
	protected boolean onFinish() {
		getItemForm().commit();
		logger.warn("item is " + getItemForm().getFormObject().toString());
		UserDetailsVO userVO = getItemForm().getFormObject();
		BCryptPasswordEncoder encoder = (BCryptPasswordEncoder) getApplicationContext().getBean("passwordEncoder");
		
		if(userVO.getPassword1() != null && userVO.getPassword1().equals(userVO.getPassword2())){
			userVO.setPassword(encoder.encode(userVO.getPassword1()));
		}
		
		ServiceUtils.getUserService().save(userVO);
		return true;
	}
	
	@Override
	protected boolean itemExists() {
		// TODO Auto-generated method stub
		return false;
	}

}
